package com.library.web.controller.library;


import com.alibaba.fastjson.JSON;
import com.library.common.annotation.Log;
import com.library.common.core.controller.BaseController;
import com.library.common.core.domain.AjaxResult;
import com.library.common.core.page.TableDataInfo;
import com.library.common.enums.BusinessType;
import com.library.common.utils.poi.ExcelUtil;
import com.library.domain.MbTradingRecord;
import com.library.service.IMbTradingRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 商品交易记录 信息操作处理
 *
 * @author wanghongjei
 * @date 2020-04-23
 * @blame Android Team
 */
@RestController
@RequestMapping("/library/mbTradingRecord")
public class MbTradingRecordController extends BaseController {
    private String prefix = "business/mobile/mbTradingRecord";

    @Autowired
    private IMbTradingRecordService mbTradingRecordService;

    @GetMapping()
    public String mbTradingRecord() {
        return prefix + "/mbTradingRecord";
    }

    @PostMapping(value = "/save", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public AjaxResult saveTradingRecord(String data) {
        List<MbTradingRecord> mbTradingRecords = JSON.parseArray(data, MbTradingRecord.class);
        int i = mbTradingRecordService.insertMbTradingRecords(mbTradingRecords);
        return AjaxResult.success();
    }

    /**
     * 查询商品交易记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(MbTradingRecord mbTradingRecord) {
        startPage();
        List<MbTradingRecord> list = mbTradingRecordService.selectMbTradingRecordList(mbTradingRecord);
        return getDataTable(list);
    }

    /**
     * 导出商品交易记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:record:export')")
    @Log(title = "商品交易记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MbTradingRecord mbTradingRecord) {
        List<MbTradingRecord> list = mbTradingRecordService.selectMbTradingRecordList(mbTradingRecord);
        ExcelUtil<MbTradingRecord> util = new ExcelUtil<MbTradingRecord>(MbTradingRecord.class);
        util.exportExcel(response, list, "商品交易记录数据");
    }

    /**
     * 获取商品交易记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(mbTradingRecordService.selectMbTradingRecordById(id.intValue()));
    }

    /**
     * 新增商品交易记录
     */
    @PreAuthorize("@ss.hasPermi('system:record:add')")
    @Log(title = "商品交易记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MbTradingRecord mbTradingRecord) {
        return toAjax(mbTradingRecordService.insertMbTradingRecord(mbTradingRecord));
    }

    /**
     * 修改商品交易记录
     */
    @PreAuthorize("@ss.hasPermi('system:record:edit')")
    @Log(title = "商品交易记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MbTradingRecord mbTradingRecord) {
        return toAjax(mbTradingRecordService.updateMbTradingRecord(mbTradingRecord));
    }

    /**
     * 删除商品交易记录
     */
    @PreAuthorize("@ss.hasPermi('system:record:remove')")
    @Log(title = "商品交易记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(mbTradingRecordService.deleteMbTradingRecordByIds(ids));
    }
}
